//===============================================================================
// Microsoft Architecture Strategy Team
// LitwareHR - SaaS Sample Application
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================



using System;
using System.Text;
using System.Configuration;
using System.Globalization;
using System.DirectoryServices.Protocols;

namespace Shp.Runtime.Services
{
    internal static class AuthenticationHelper
    {
        const string AdsHost = "AD_Host";
        const string AdsPort = "AD_Port";
        const string AdsInstanceName = "AD_InstanceName";

        internal static string Host
        {
            get
            {
                return ConfigurationManager.AppSettings[AdsHost];
            }
        }

        internal static string Port
        {
            get
            {
                return ConfigurationManager.AppSettings[AdsPort];
            }
        }

        internal static string InstanceName
        {
            get
            {
                return ConfigurationManager.AppSettings[AdsInstanceName];
            }
        }

        internal static string GetConnectionString()
        {
            return GetConnectionString(String.Empty, null);
        }

        internal static string GetConnectionString(string parameters, params string[] args)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("LDAP://");
            sb.Append(AuthenticationHelper.Host);
            sb.Append(":");
            sb.Append(AuthenticationHelper.Port);
            sb.Append("/");

            if (!String.IsNullOrEmpty(parameters))
            {
                sb.AppendFormat(CultureInfo.InvariantCulture, parameters, args);
                sb.Append(",");
            }
            
            sb.Append("O=");
            sb.Append(AuthenticationHelper.InstanceName);

            return (sb.ToString());
        }

        internal static LdapConnection GetLdapConnection(string connectionString)
        {
            Uri uri = new Uri(connectionString);
            LdapDirectoryIdentifier didentifier = new LdapDirectoryIdentifier(uri.Host + ":" + uri.Port);
            LdapConnection ldap = new LdapConnection(didentifier);
            ldap.AuthType = AuthType.Basic;
            ldap.SessionOptions.ProtocolVersion = 3;
            return ldap;
        }
    }
}
